package com.sscl.easyble.receiver

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.sscl.easyble.Logger
import com.sscl.easyble.variable.GlobalVariable

internal class BluetoothStateReceiver : BroadcastReceiver() {

    private companion object{
        private const val TAG = "BluetoothStateReceiver"
    }

    init {
        Logger.log(TAG, "蓝牙状态变化广播接收器 init")
    }


    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 实现方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action ?: return
        if (BluetoothAdapter.ACTION_STATE_CHANGED == action) {
            when (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)) {
                BluetoothAdapter.STATE_OFF -> {
                    Logger.log(TAG,"BluetoothAdapter.STATE_OFF")
                    GlobalVariable.onBluetoothStateChangedListener?.onBluetoothDisable()
                }

                BluetoothAdapter.STATE_ON -> {
                    Logger.log(TAG,"BluetoothAdapter.STATE_ON")
                    GlobalVariable.onBluetoothStateChangedListener?.onBluetoothEnable()
                }

                BluetoothAdapter.STATE_TURNING_OFF -> {
                    Logger.log(TAG,"BluetoothAdapter.STATE_TURNING_OFF")
                    GlobalVariable.onBluetoothStateChangedListener?.onBluetoothDisabling()
                }

                BluetoothAdapter.STATE_TURNING_ON -> {
                    Logger.log(TAG,"BluetoothAdapter.STATE_TURNING_ON")
                    GlobalVariable.onBluetoothStateChangedListener?.onBluetoothEnabling()
                }
            }
        }
    }
}